package client.comm.baoding.ui.vm

import android.app.Application
import androidx.lifecycle.*
import client.comm.baoding.ui.viewmodel.MiddleVm
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class TestViewModel(app: Application) : MiddleVm(app) {
    val isShowing = MutableLiveData<Boolean>(false)
    private val _myUiState = MutableStateFlow<String>("0")
    val myUiState: StateFlow<String> = _myUiState

    val result: StateFlow<String> = flow<String> {
        delay(5000)
        emit("userId sfds")
    }.flowOn(Dispatchers.IO).mapLatest { newUid ->
        newUid
    }.stateIn(
        scope = viewModelScope,
        started = SharingStarted.WhileSubscribed(),
        initialValue = "121"
    )

    fun test() {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                delay(1000)
            }

            _myUiState.value = "2"
        }
    }
}